package com.system.netty.change.chat.server;

import com.system.netty.change.chat.handler.ServerChanelHandle;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;


public class  DiscardServer{

    public void openServer(int port){
        ServerBootstrap bootstrap = new ServerBootstrap();
        EventLoopGroup boss = new NioEventLoopGroup(1);  //create boss group, threadpool size is 1
        EventLoopGroup work = new NioEventLoopGroup(5); //create work group, threadpool size is 5
        bootstrap.group(boss,work);   //组合netty组件
        //配置handle组件
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                //编码格式
                ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
                //解码格式
                ch.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
                ch.pipeline().addLast(new ServerChanelHandle());
            }
        });
        bootstrap.channel(NioServerSocketChannel.class);

        try{
            ChannelFuture channel = bootstrap.bind(port).sync();
            System.out.println(("服务端已启动，绑定端口:" + port));
            channel.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            boss.shutdownGracefully();
            work.shutdownGracefully();
        }

        bootstrap .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);
    }

    public static void main(String[] args){
        DiscardServer server = new DiscardServer();
        server.openServer(8090);
    }

}
